package programManager;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.ArrayList;
import javax.sound.midi.*;

public class Helper{
	
	static HashSet<String> Dictionary = new HashSet<String>();
	static Hashtable<String,ArrayList<int[]>> noteList = new Hashtable<String,ArrayList<int[]>>();
	static float checkSum = 0;
	static float compas = 0;
	static int octave = 0;
	static int gcount = 0;
	static Track[] track = null;
	static int tempo = 0; 
	static int time = 0; 
	static String fileName;
	
	private static final int	VELOCITY = 64;
	public static Sequence sq = null;
	
	public static void init(){
		
		try {
			 sq = new Sequence(Sequence.PPQ, tempo,10);
			 track = sq.getTracks();
			
		} catch (InvalidMidiDataException e) {
			e.printStackTrace();
		}	
	}
	
	public static void setOctave(int i){
		octave = i;
	}
	
	public static MidiEvent createNoteOnEvent(int nKey, int lTick, int counter)
	{
		//gcount = counter;
		return createNoteEvent(ShortMessage.NOTE_ON, nKey, VELOCITY, counter);
	}

	public static MidiEvent createNoteOffEvent(int nKey, int lTick, int counter){
		//gcount = counter;
		int x = calculateCounter(lTick);
		gcount = gcount + x;
		return createNoteEvent(ShortMessage.NOTE_OFF, nKey, 0, gcount);
	}
	public static int calculateCounter(int c){
		int v = 0;
		switch(c){
			case 1: v = 64;
			break;
			case 2: v = 32;
			break;
			case 4: v = 16;
			break;
			case 8: v = 8;
			break;
			case 16: v = 4;
			break;
			case 32: v = 2;
			break;
			case 64: v = 1;
			break;
			default:
			break;
		}
		return v;
	}
	
	public static MidiEvent createNoteEvent(int nCommand, int nKey, int nVelocity, long lTick){
		ShortMessage message = new ShortMessage();
		try{
			message.setMessage(nCommand,
			  0,	// always on channel 1
			  nKey,
			  nVelocity);
		}
		catch (InvalidMidiDataException e){
			e.printStackTrace();
		}
		MidiEvent	event = new MidiEvent(message, lTick);
		return event;
	}
	
	public static int calculateNumberNote(String lNote){
		int note = 3;
		lNote = lNote.toLowerCase();
		if(octave == 0){
				switch(lNote){
					case "la":	;
					case "a":	note = 21;  break;
					case "si":	;
					case "b":	note = 23;  break;
				default: 
					break;
				}
		}else if(octave > 0 && octave < 8)	{	
			note = (12*(octave+1));
			switch (lNote) {
				case "re":	;
				case "d":	note += 2;  break;
				case "mi":	;
				case "e":	note += 4;  break;
				case "fa":	;
				case "f":	note += 5;  break;
				case "sol":	;
				case "g":	note += 7;  break;
				case "la":	;
				case "a":	note += 9;  break;
				case "si":	;
				case "b":	note += 11;  break;
			default:
				break;
			}
		}else if(octave == 8){
			switch (lNote) {
				case "do":	;
				case "c":	note = 108;  break;
				default :
					break;
			}
		}
		return note;
	}
	
	// public static int calculateNumberNote(String lNote, int lOctave){
		// int note;
		// lNote = lNote.toLowerCase();
		// note = (12*(lOctave+1));
		
		// switch (lNote) {
		// case "re":	note += 2;  break;
		// case "mi":	note += 4;  break;
		// case "fa":	note += 5;  break;
		// case "sol":	note += 7;  break;
		// case "la":	note += 9;  break;
		// case "si":	note += 11;  break;
		// default:
			// break;
		// }
		
		
		// return note;
	// }
	
	public static int calculateNumberNote(String lNote, char bMol){
		int note = 3;
		lNote = lNote.toLowerCase();
		if(octave == 0){
			if (bMol == '&') {
				switch(lNote){
					case "si":	;
					case "b":	note = 23 - 1;  break;
				default :
					break;
				}
			}else if(bMol == '#'){
				switch(lNote){
					case "la":	;
					case "a":	note = 21 + 1;  break;
					case "si":	;
					case "b":	note = 23 + 1;  break;
				default: 
					break;
				}
			}
		}
		else if(octave > 0 && octave < 8){
			note = (12*(octave+1));
			if (bMol == '&') {
				switch (lNote) {
				case "re":	;
				case "d":	note += 2 - 1;  break;
				case "mi":	;
				case "e":	note += 4 - 1;  break;
				case "fa":	;
				case "f":	note += 5 - 1;  break;
				case "sol":	;
				case "g":	note += 7 - 1;  break;
				case "la":	;
				case "a":	note += 9 - 1;  break;
				case "si":	;
				case "b":	note += 11 - 1;  break;
				default:
							note -= 1;
					break;
				}
			}else if(bMol == '#'){
				switch (lNote) {
				case "re":	;
				case "d":	note += 2 + 1;  break;
				case "mi":	;
				case "e":	note += 4 + 1;  break;
				case "fa":	;
				case "f":	note += 5 + 1;  break;
				case "sol":	;
				case "g":	note += 7 + 1;  break;
				case "la":	;
				case "a":	note += 9 + 1;  break;
				case "si":	;
				case "b":	note += 11 + 1;  break;
				default:
							note += 1;
					break;
				}
			}	
		}else if(octave == 8 ){
				if (bMol == '&') {
					switch(lNote){
						case "do":	;
						case "c":	note = 108 - 1;  break;
					default :
						break;
					}
				}
		}
		return note;
	}
	
	
	
	public static void setCompas(float v){
		compas = v;
	}
	
	public static float getCompas(){
		return compas;
	}
	
	public static void setSum(float v){
		checkSum = v;
	}
	
	public static float getSum(){
		return checkSum;
	}
	
	public static void addSum(float v){
		checkSum += v;
	}
	
	public static int getOctave(){
		return octave;
	}
	
	public static void setTempo(int t){
		tempo = (int)((240/(210-t))*32);
	}
	
}